<?php
declare(strict_types=1);

namespace app\job;

use app\common\model\activity\LuckyBag;
use app\common\service\lucky\Bag;
use think\facade\Log;
use think\queue\Job;

/**
 * 福袋福箱处理
 */
class LuckyBagBox
{
    const QUEUE = "lucky_bag_box";

    public function fire(Job $job, array $data)
    {
        $activity = LuckyBag::where(['activity_id' => $data['activity_id']])->find();
        if (!$activity) {
            $job->delete();
        }
        try {
            $res = (new Bag())->addLuckBox($data,$activity);
            if ($res) {
                $job->delete();
            }
            if ($job->attempts() > 3) {
                $job->delete();
            }
        } catch (\Exception $exception) {
            $job->delete();
            Log::channel('queue')->write(self::QUEUE . " 福袋活动ID:" . $data['activity_id'] . "创建福箱商品失败==> error:" . $exception->getTraceAsString());
        }
    }
}